package com.aaa.ps.service;

import com.aaa.common.entity.Menu;
import com.aaa.common.vo.TreeNode;
import com.aaa.ps.dao.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * fileName:MenuServiceImpl
 * description:
 * author:zz
 * createTime:2020/8/25 16:28
 * version:1.0.0
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public int deleteByPrimaryKey(Integer menuId) {
        return menuMapper.deleteByPrimaryKey(menuId);
    }

    @Override
    public int insertSelective(Menu record) {
        return menuMapper.insertSelective(record);
    }

    @Override
    public Menu selectByPrimaryKey(Integer menuId) {
        return menuMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public int updateByPrimaryKeySelective(Menu record) {
        return menuMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Menu> selectAll() {
        return menuMapper.selectAll();
    }

    @Override
    public List<TreeNode> treeData() {
        //获取全部数据
        List<TreeNode> treeNodes = menuMapper.selectTreeData();
        return getTreeData(treeNodes);
    }

    @Override
    public List<TreeNode> treeDataByUserId(int userId) {
        //获取全部数据
        List<TreeNode> treeNodes = menuMapper.selectTreeDataByUserId(userId);
        return getTreeData(treeNodes);
    }

    /**
     * 封装返回数据
     * @param treeNodes
     * @return
     */
    private  List<TreeNode>  getTreeData(List<TreeNode> treeNodes){
        //定义返回集合
        List<TreeNode> returnList  =new ArrayList<>();
        //判断是否为空
        if(treeNodes!=null&&treeNodes.size()>0){
            //循环
            for (TreeNode treeNode : treeNodes) {
                //判断是否是一级节点
                if(treeNode.getParentId()==0){
                    returnList.add(treeNode);
                    //查找子节点（递归）
                    bindChildren(treeNode,  treeNodes);
                }
            }
        }
        return returnList;
    }

    /**
     *  绑定子节点（递归）
     * @param currenTreeNode
     * @param treeNodes
     */
    private  void bindChildren(TreeNode currenTreeNode,  List<TreeNode> treeNodes){
        //循环集合
        for (TreeNode treeNode : treeNodes) {
            //判断循环节点的父ID是否和当前节点的ID相等，如果相等，说明当前循环节点是当前的节点孩子
            if(treeNode.getParentId()==currenTreeNode.getId()){
                //获取当前节点所有孩子
                List<TreeNode> children = currenTreeNode.getChildren();
                //每一个节点，如果有子节点，第一个子节点被加入时，孩子节点集合肯定为空
                if(children==null){
                    children = new ArrayList<>();
                }
                //把当前循环节点放入孩子集合
                children.add(treeNode);
                //设置孩子集合
                currenTreeNode.setChildren(children);
                //递归调用自己，为当前循环节点继续找孩子
                bindChildren(treeNode,treeNodes);
            }
        }
    }

}
